package io.gitee.xyuesheng.day07;

import org.junit.Test;

import java.io.*;
import java.util.Date;

public class TestStream {
    //使用字节流读文件
    @Test
    public void testByteStream() throws IOException {
//        FileNotFoundException
        InputStream is = new FileInputStream("data\\sentence.txt");

//        IOException
        int letter = is.read();

        System.out.println((char) letter);

        //一次读多个字节

        byte[] bytes = new byte[10];
        is.read(bytes);//读10个字节到bytes容器
        //根据字节数组构建字符串
        System.out.println(new String(bytes));

        //流是连续的

        //使用循环把流里面的数据读完
        while (true) {
            int c = is.read();

            if (c == -1) {
                break;
            }
            System.out.println((char) c);

        }

//        int i=0;
//        while( (i=is.read())!=-1){
//            System.out.println((char)i);
//        }

    }

    @Test
    public void test() {
        try (InputStream is = new FileInputStream("data\\sentence.txt")) {
            byte[] bytes = new byte[10];

            while (true) {
                int count = is.read(bytes);

                if (count == -1) {
                    break;
                }

                System.out.println(new String(bytes, 0, count));

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //IO资源要释放
    @Test
    public void testCloseStream() {
        //{}里面是一个新的作用域：块级作用域
        InputStream is = null;
        try {
            System.out.println("1###");
            is = new FileInputStream("data\\sentence.txt");
            System.out.println("2###");

            while (true) {
                int c = is.read();
                if (c == -1) {
                    break;
                }
                System.out.println((char) c);
            }
            //不要在这里关
            //is.close();
        } catch (FileNotFoundException e) {
            System.out.println("3###");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("4###");
            if (is != null) {//is可能为null，因为文件不存在
                try {
                    is.close();//关闭资源的时候也可能出异常
                } catch (IOException e) {
                    //
                }
            }
        }
    }

    @Test
    public void testCloseStream1() {
        //把资源在try后面用（）初始化，资源会自动关闭
        try (InputStream is = new FileInputStream("data\\sentence.txt")) {
            while (true) {
                int c = is.read();
                if (c == -1) {
                    break;
                }
                System.out.println((char) c);
            }
            //不要在这里关
            //is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //输出流
    @Test
    public void testOutput() {
        //第二个参数，表示往文件里面添加内容
        try (OutputStream os = new FileOutputStream("data\\sentence.txt")) {
            os.write('A');

            byte[] bytes = new byte[10];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) ('A' + i);
            }
            os.write(bytes);
            //
            /**
             *字符串可以和字节数组相互转换
             * String s=new String(bytes);=》字节数组转换成字符串
             * s.getBytes();=》字符串转为字节数组
             */
            os.write("hello world".getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testCopy() {
        Files.copy("D:\\RJ\\java\\data\\sentence.txt", "D:\\b.txt");
//        Files.copy(" ","");
    }

    @Test
    public void testData() {
        System.out.println(new Date().getTime());

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(new Date().getTime());

    }

    @Test
    public void testCopyBreak() {

        Files.copyBreak("", "");
    }

}
